package ui
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class UIRadioTabGroup extends Sprite implements Interactivable
	{
		public var tabs:Vector.<UIRadioTab>;
		
		public var selecting:int = -1;
		
		public function UIRadioTabGroup(count:int, w:int, h:int)
		{
			super();
			tabs = new Vector.<UIRadioTab>(count);
			var tab:UIRadioTab;
			for (var i:int = 0; i < count; i++) {
				tab = tabs[i] = new UIRadioTab(w, h);
				tab.x = i * (w-16);
				tab.id = i;
				addChild(tab);
			}
			select(0);
		}
		
		public function onActive():void {
			for each (var tab:UIRadioTab in tabs) {
				tab.addEventListener(MouseEvent.ROLL_OVER, onMouseOverTab);
				tab.addEventListener(MouseEvent.ROLL_OUT, onMouseOutTab);
				tab.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownTab);
				tab.addEventListener(MouseEvent.MOUSE_UP, onMouseUpTab);
			}
		}
		
		private function select(v:int):void {
			if (selecting == v) return;
			for (var i:int = 0; i < tabs.length; i++) {
				if (i != v) {
					tabs[i].unselect();
					addChildAt(tabs[i], 0);
				}				
			}
			tabs[v].select();
			selecting =  v;
			addChild(tabs[v]);
		}
		
		private function onMouseOverTab(e:Event):void {
			var tab:UIRadioTab = e.target as UIRadioTab;
		}
		
		private function onMouseOutTab(e:Event):void {
			var tab:UIRadioTab = e.target as UIRadioTab;
		}
		
		private function onMouseDownTab(e:Event):void {
			var tab:UIRadioTab = e.target as UIRadioTab;
			select(tab.id);
		}
		
		private function onMouseUpTab(e:Event):void {
			var tab:UIRadioTab = e.target as UIRadioTab;
		}

		public function onUnActive():void {
			for each (var tab:UIRadioTab in tabs) {
				tab.removeEventListener(MouseEvent.ROLL_OVER, onMouseOverTab);
				tab.removeEventListener(MouseEvent.ROLL_OUT, onMouseOutTab);
				tab.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownTab);
				tab.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpTab);
			}
		}
		
	}
}